package com.cn.ccp.action.system;

import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.FacultyBean;
import com.cn.ccp.bean.MenuManageBean;
import com.cn.ccp.bean.RoleBean;
import com.cn.ccp.util.CCPContants;
import com.cn.ccp.util.MsgConstants;

public class MenuManageAction extends BaseAction {
	private Logger log = Logger.getLogger(MenuManageAction.class);
	
	private String searchUserNumber ;
	private String searchUserName ;
	private String searchUserType ;
	private String searchRoleId ;
	private SelectItem[] roleSelectItem ;
	private String searchFacultyId ;
	private SelectItem[] facultySelectItem ;
	
	
	private List<MenuManageBean> menuManageLlist ;
	private String userid ;
	
	
	private SelectItem[] selectRoleItem ;
	private SelectItem[] noSelectRoleItem ;
	private String selectRoleStr = "" ;
	
	
	
	
	
	
	public MenuManageAction(){
		
	}

	public void searchClear(){
		searchUserNumber = "" ;
		searchUserName = "" ;
		searchUserType = "-1" ;
		searchRoleId = "-1" ;
		searchFacultyId = "-1" ;
	}
	
	
	public String showUserRole(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		if(id == null || id.equals("null") || id.equals(""))
			return "";
		userid = id ;
		if(userid.equals("1") && !CCPContants.getUserBean().getUserName().equalsIgnoreCase("admin")){
			return "";
		}
		List<RoleBean> list = getRoleService().getUserRoleList(Integer.parseInt(userid));
		selectRoleItem = new SelectItem[list.size()];
		for (int i = 0; i < list.size(); i++) {
			selectRoleItem[i] = new SelectItem(""+list.get(i).getId() , list.get(i).getRolename());
			selectRoleStr += list.get(i).getId()+"," ;
		}
		
		list = getRoleService().getUserNoRoleList(Integer.parseInt(userid));
		noSelectRoleItem = new SelectItem[list.size()];
		for (int i = 0; i < list.size(); i++) 
			noSelectRoleItem[i] = new SelectItem(""+list.get(i).getId() , list.get(i).getRolename());
		return UPDATE ;
	}
	
	public void search(){
		getpagedetail();
	}
	
	public void delete(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		if(id == null || id.equals("null") || id.equals(""))
			return ;
		if(id.equals("1") && !CCPContants.getUserBean().getUserName().equalsIgnoreCase("admin")){
			return ;
		}
		boolean flag = getMenuManageService().delete(id);
		if(flag){
			getpagedetail();
			showMsg("listform",  getMessages("menu_mange_page_delete_success"));
		}else{
			showMsg("listform",  getMessages("menu_mange_page_delete_error"));
		}
	}
	
	public String submitUserRole(){
		if(userid != null  && !userid.equals("")){
			boolean flag = getMenuManageService().insertUserRole(userid, selectRoleStr);
			if(flag)
				showMsg("listform",  MsgConstants.MENU_MANAGE_SET_USER_ROLE_SUCCESS);
			else
				showMsg("listform",  MsgConstants.MENU_MANAGE_SET_USER_ROLE_ERROR);
		}else{
			showMsg("listform",  MsgConstants.MENU_MANAGE_SET_USER_ROLE_SUCCESS);
		}
		getpagedetail();
		return MAIN ;
	}
	
	@Override
	public void getpagedetail() {
		log.info("---------searchUserNumber = "+searchUserNumber);
		log.info("---------searchUserName = "+searchUserName);
		log.info("---------searchUserType = "+searchUserType);
		log.info("---------searchRoleId = "+searchRoleId);
		log.info("---------searchFacultyId = "+searchFacultyId);
		setItemCount(getMenuManageService().getCount(searchUserNumber , searchUserName , searchUserType , searchRoleId , searchFacultyId));
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		if(start < 0)
			start = 0 ;
		menuManageLlist = getMenuManageService().getAll(start, pagesize, searchUserNumber , searchUserName , searchUserType , searchRoleId , searchFacultyId);
	}

	public String getSearchUserNumber() {
		return searchUserNumber;
	}

	public void setSearchUserNumber(String searchUserNumber) {
		this.searchUserNumber = searchUserNumber;
	}

	public String getSearchUserName() {
		return searchUserName;
	}

	public void setSearchUserName(String searchUserName) {
		this.searchUserName = searchUserName;
	}

	public String getSearchUserType() {
		return searchUserType;
	}

	public void setSearchUserType(String searchUserType) {
		this.searchUserType = searchUserType;
	}

	public String getSearchRoleId() {
		return searchRoleId;
	}

	public void setSearchRoleId(String searchRoleId) {
		this.searchRoleId = searchRoleId;
	}

	public SelectItem[] getRoleSelectItem() {
		if(roleSelectItem == null){
			List<RoleBean> list = getRoleService().queryAll();
			roleSelectItem = new SelectItem[list.size()];
			for (int i = 0; i < list.size(); i++) 
				roleSelectItem[i] = new SelectItem(""+list.get(i).getId() , list.get(i).getRolename());
		}
		return roleSelectItem;
	}

	public void setRoleSelectItem(SelectItem[] roleSelectItem) {
		this.roleSelectItem = roleSelectItem;
	}

	public String getSearchFacultyId() {
		return searchFacultyId;
	}

	public void setSearchFacultyId(String searchFacultyId) {
		this.searchFacultyId = searchFacultyId;
	}

	public SelectItem[] getFacultySelectItem() {
		if(facultySelectItem == null ){
			List<FacultyBean> list = getFacultyService().getAllFaculty();
			facultySelectItem = new SelectItem[list.size()];
			for (int i = 0; i < list.size(); i++) 
				facultySelectItem[i] = new SelectItem(""+list.get(i).getId() , list.get(i).getFacultyName());
		}
		return facultySelectItem;
	}

	public void setFacultySelectItem(SelectItem[] facultySelectItem) {
		this.facultySelectItem = facultySelectItem;
	}

	public List<MenuManageBean> getMenuManageLlist() {
		if(menuManageLlist == null)
			getpagedetail();
		return menuManageLlist;
	}

	public void setMenuManageLlist(List<MenuManageBean> menuManageLlist) {
		this.menuManageLlist = menuManageLlist;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public SelectItem[] getSelectRoleItem() {
		if(selectRoleItem == null)
			selectRoleItem = new SelectItem[0];
		return selectRoleItem;
	}

	public void setSelectRoleItem(SelectItem[] selectRoleItem) {
		this.selectRoleItem = selectRoleItem;
	}

	public SelectItem[] getNoSelectRoleItem() {
		if(noSelectRoleItem == null)
			noSelectRoleItem = new SelectItem[0];
		return noSelectRoleItem;
	}

	public void setNoSelectRoleItem(SelectItem[] noSelectRoleItem) {
		this.noSelectRoleItem = noSelectRoleItem;
	}

	public String getSelectRoleStr() {
		return selectRoleStr;
	}

	public void setSelectRoleStr(String selectRoleStr) {
		this.selectRoleStr = selectRoleStr;
	}
}
